<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 视频控制器
 */
class Video extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('video_model');
        $this->assign("module", "video");
    }
    
    //视频列表页
    function index() {

        $item = $this->db->query("select * from tan_video where status = 1 order by sort desc limit 1")->row_array();
        //$this->db->query("UPDATE tan_go_shoplist SET q_uid = Null WHERE id IN (1,2,3,22)");
        web_redirect("video/".$item['salt']);
        $this->display('video/index.html');
    }
    //视频详情页
    function detail($video_salt,$comment_id){
        $item = $this->db->query("select * from tan_video where salt = '$video_salt' and status = 1 order by sort desc limit 1")->row_array();
        if(!$item){//如果视频不存在
            $this->assign("error", "您查找的资源不存在或已被删除");
            $this->display('deleted/index.html');exit;
        }
        if($comment_id){//指定查看某个评论
            $comment_id = decrypt_comment_id($comment_id);
            $target_comment = $this->db->query("select * from tan_video_comment where id = $comment_id and video_id = $item[id] and is_show = 1 order by id desc")->num_rows(); 
            if(!$target_comment){//如果评论不存在
                $this->assign("error", "您查找的回复不存在或已被删除");
                $this->display('deleted/index.html');exit;
            }
        }
        //判断是否收藏
        $is_favorite = $this->bbs_userinfo ? ($this->db->query("select * from tan_video_favorite where user_id = ".$this->bbs_userinfo['user_id']." and video_id = $item[id] order by id desc limit 1")->row_array() ? 1 : 0) : 0;
        //判断是否喜欢或讨厌
        $result = $this->bbs_userinfo ? $this->db->query("select * from tan_video_like where video_id = '".$item['id']."' and user_id ='".$this->bbs_userinfo['user_id']."' order by id desc limit 1")->row_array() : array();
        $item['is_like'] = $result ? $result['status'] : 0;
        $item['is_hate'] = $result ? !$result['status'] : 0;
        
        $item['is_favorite'] = $is_favorite;
        $item['is_like_or_hate_invilib'] = $result ? 1 : 0;//判断是否可以进行喜欢或讨厌操作
        $item['video_id'] = encrypt_video_id($item['id']);
        $item['share_url'] = share_url("video/".$item['salt']);
        //精华评论
        $hot_comments = $this->get_hot_comments($item['video_id']);
        
        $reward = $this->db->select("r.user_id,r.target_id,r.create_time,u.head_img,u.nickname")
                ->from("bbs_reward r")
                ->join("users_info u","u.user_id = r.user_id","left")
                ->where(array("r.target_id"=>$item['id'],"r.target_type"=>'video'))->order_by("r.id desc")->group_by("r.user_id")->get()->result_array();
        foreach ($reward as $key=>$val){
            $reward[$key]['space_url'] = web_url("u/".encrypt_user_id($val['user_id']));
        }
        $this->assign('reward', $reward);
        
        
        $this->assign("site_title", $item['title']);
        $this->assign('item', $item);
        $this->assign('hot_comments', $hot_comments);
        $this->assign("comment_id",$target_comment ? encrypt_comment_id($comment_id) : "");
        $this->display('video/detail.html');
    }
    //获取精华评论
    private function get_hot_comments(){
        $list = array();
        return $list;
    }

    function video() {
       /* $video_id = $_GET['video_id'];
        $id = decrypt_video_id($video_id);
        $item = $this->db->query("select * from tan_video where status = 1 and id='".$id."' order by sort desc limit 1")->row_array();*/
        $this->display('video/url.html');
    }
}
